<!DOCTYPE html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<html>
<head>
<title>角色管理-编辑角色</title>
<jsp:include page="/common/head.jsp"></jsp:include>
</head>
<body class="iframe-page">
<div class="page-title-wrapper">
    <div class="page-title">
        <div class="page-title-text">
            <span>角色管理</span>
            <span class="split"></span>
            <span>编辑角色</span>
        </div>
        <a id="goBack" class="btn-return distance-left" href="javascript:;"><i class="icon icon-arrow-left"></i>返回上级</a>
    </div>
    <div class="page-title-border"></div>
</div>
<form id="editForm" method="post">
<div class="form-wrapper">
    <div class="form-cont open">
        <div class="form-cont-row">
            <input type="hidden" id="roleId" name="id" class="summer-input" value="${role.id}"/> 
            <p class="form-cont-title"><span class="field-required">*</span>角色名称</p>
            <input type="text" id="roleName" name="name" class="summer-input" value="${role.name}"/> 
            <p class="label-secondary"></p>
        </div>           
    </div>
</div>        

<div class="form-btn-wrapper" id="funcbar">
    <a class="btn btn-normal" id="closeBtn">关闭</a>
</div>
</form>
<script>
    var ROLE_CONST_URL = {
        SUBMIT : basePath + "/sys/role/edit",
        CHECKEXIST :basePath + "/sys/role/checkRoleExist",
        INIT: basePath + "/sys/role/init"
    };
    var permission = CommonUtil.getPermission('role');

    $(function() {
    	$('#funcbar').prepend($.funcbar({
            type: 'button',
            data: permission,
            permit: ['role:edit']
        }));  
               
         //表单提交事件
         $('#funcbar').on('click', 'a[data-permit="role:edit"]', save);
        
        //关闭按钮事件
        $('#closeBtn,#goBack').on('click',goBack);
        
        //校验角色名称是否存在        
        validateExist('#roleName','name', ROLE_CONST_URL.CHECKEXIST, ['required','maxLength[30]', "chsNoSymbol", 'regexp'], "角色名可用");
    });
    
    //保存
    function save() {
        $("#editForm").form({
            onSubmitSuccess: function (data) {
                if (data.result === "success") {
                	$.stip({
                        type:'ok',//显示类型 有info、 ok、 danger
                        text: '保存成功！',
                        onBeforeHide: function() {
                            goBack();
                        }
                    });
                }
            },
            ajax: {
                url : ROLE_CONST_URL.SUBMIT
            }
        }).form("submit");
    }
    
    //关闭
    function goBack() {
    	window.location.href=ROLE_CONST_URL.INIT;
    }
    
    //校验角色名称是否存在
    function validateExist(id, name, url, validType, message) {
        $(id).validatebox({
            rules: {
                regexp: {
                    validator: function (value,param) {
                        var result = false,
                            params = {}, 
                            key = name;
                            params[key] = value;
                            params['id'] = $('#roleId').val();//过滤正在编辑的数据
                        $.ajax({
                            type: 'post',
                            url: url,
                            data: params,
                            dataType: 'json',
                            async: false,
                            success: function (data) {
                                if (data.result == 'exist') {
                                    result = false;
                                } else {
                                    result = true;
                                }
                            }
                        });
                        return result;
                    },
                    message: message,
                }
            },
            validType: validType
        });
    }
</script>
</body>
</html>